package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.common.utils.TrendUtil;
import com.kaizzi.data.jdl.been.TrendBeen;
import com.kaizzi.data.jdl.model.CommentsModel;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.model.Page8Model;
import com.kaizzi.data.jdl.service.CrmMemberService;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/14
 */
@Component
public class PageData8 {

    @Resource
    private CrmMemberService crmMemberService;


    public String getData(ExcelModel model){
        List<Page8Model> page8s = model.getPage8s();
        List<TempPage8> tempPage8s = new ArrayList<>();
        Page8 page8 = new Page8();

        //page8 表格内数据
        for (Page8Model model1:page8s) {
            TempPage8 tempPage8 = new TempPage8();
            tempPage8.setDate(new TrendBeen(DateUtil.getTransTime5(model1.getDate()),0));
            tempPage8.setOtherApp(new TrendBeen(model1.getOtherApp(),0));
            tempPage8.setConversation(new TrendBeen(model1.getConversation(),0));
            tempPage8.setTaskUsed(new TrendBeen(model1.getTaskUsed(),0));
            tempPage8.setSearch(new TrendBeen(model1.getSearch(),0));
            tempPage8.setTaskMyApp(new TrendBeen(model1.getTaskMyApp(),0));
            tempPage8.setPubMenu(new TrendBeen(model1.getPubMenu(),0));
            tempPage8.setPubArticle(new TrendBeen(model1.getPubArticle(),0));
            tempPage8.setQrCode(new TrendBeen(model1.getQrCode(),0));
            tempPage8.setScanning(new TrendBeen(model1.getScanning(),0));

            tempPage8s.add(tempPage8);
        }

        List<TempChannelSource> channels = new ArrayList<>();
        List<TempNatureSource> natures = new ArrayList<>();

        for (String date:DateUtil.getDateList(2)) {
            List<String> dateList= DateUtil.getTransTime(date);
            TempNatureSource nature = new TempNatureSource();
            long all = crmMemberService.getUserCount(dateList.get(0),dateList.get(1),"");
            long utc = crmMemberService.getUserCount(dateList.get(0), dateList.get(1), "utc");
            long wes = crmMemberService.getWes(dateList.get(0), dateList.get(1));
            long natureCount = all - utc - wes;

            nature.setDate(new TrendBeen(DateUtil.getTransTime5(date),0));
            nature.setUtcCount(new TrendBeen(""+ utc,0));
            nature.setNaturalCount(new TrendBeen( natureCount +"",0));
            nature.setWesCount(new TrendBeen(""+wes,0));
            natures.add(nature);

            TempChannelSource channel = new TempChannelSource();
            channel.setDate(new TrendBeen(DateUtil.getTransTime5(date),0));
            long king = crmMemberService.getKing(dateList.get(0), dateList.get(1));
            long teamCount = crmMemberService.getTeamCount(dateList.get(0), dateList.get(1));
            long appletCount = natureCount - king - teamCount;
            channel.setAppletCount(new TrendBeen(""+appletCount,0));
            channel.setKing(new TrendBeen(""+king,0));
            channel.setTeamCount(new TrendBeen(""+teamCount,0));
            channels.add(channel);
        }

        TrendUtil.trendSetting(tempPage8s);
        page8.setPage8s(tempPage8s);
        page8.setTitle(LogicUtil.getTitle(model, 8));
        TrendUtil.trendSetting(channels);
        page8.setUserChannelSources(channels);
        TrendUtil.trendSetting(natures);
        page8.setUserNatureSources(natures);
        page8.setDataPeriod(DateUtil.getLastWeekDate());
        return JsonUtil.serialize(page8);
    }


    @Data
    private static class TempPage8{
        private TrendBeen date;
        private TrendBeen otherApp;
        private TrendBeen conversation;
        private TrendBeen taskUsed;
        private TrendBeen search;
        private TrendBeen taskMyApp;
        private TrendBeen pubMenu;
        private TrendBeen pubArticle;
        private TrendBeen qrCode;
        private TrendBeen scanning;
    }

    @Data
    private static class TempChannelSource{
        private TrendBeen date;
        private TrendBeen appletCount;
        private TrendBeen King;
        private TrendBeen teamCount;
    }

    @Data
    private static class TempNatureSource{
        private TrendBeen date;
        private TrendBeen naturalCount;
        private TrendBeen utcCount;
        private TrendBeen wesCount;
    }

    @Data
    private static class Page8{
        private String title;
        private String dataPeriod;
        private List<TempChannelSource> userChannelSources;
        private List<TempNatureSource> userNatureSources;
        private List<TempPage8> page8s;
    }
}
